import {BaseApi} from "./BaseApi.ts";
import {buildTopicListData, type TopicListData} from "../types/Topic.ts";
import type {NgaResponse} from "../types/NgaResponse.ts";


export enum TopicOrderBy {
    Post = 'postdatedesc',
    Last = 'lastpostdesc',
}

export enum SearchContent {
    Content = 1,
    Reply = 8,
    Title = 4,
}


export class TopicApi extends BaseApi {
    /**
     * 查询版面主题列表
     * @param fid 版面ID
     * @param page 页码
     * @param orderBy 排序方式
     */
    forumList = (fid: number, page?: number, orderBy?: TopicOrderBy) => this.thread({fid, page, orderBy})
    /**
     * 查询版面精华
     * @param fid 版面ID
     * @param page 页码
     */
    forumRecommend = (fid: number, page?: number) => this.thread({fid, page, recommend: 1})
    /**
     * 查询版面搜索
     * @param key 关键字
     * @param fid 版面ID
     * @param page 页码
     * @param content 搜索内容
     * @param recommend 是否精华
     */
    forumSearch = (key: string, fid: number[], page?: number, content?: SearchContent, recommend?: 1 | 0) => this.thread({
        key,
        fid: fid.join(','),
        page,
        content,
        recommend
    })
    /**
     * 浏览合集主题列表
     * @param colId 合集ID
     * @param page 页码
     * @param orderBy 排序方式
     */
    colList = (colId: number, page?: number, orderBy?: TopicOrderBy) => this.thread({stid: colId, page, orderBy})

    /**
     * 浏览合集搜索
     * @param key 关键字
     * @param colId 合集ID
     * @param page 页码
     * @param content 搜索内容
     * @param recommend 是否精华
     */
    colSearch = (key: string, colId: number, page?: number, content?: SearchContent, recommend?: 1 | 0 | undefined) => this.thread({
        key,
        stid: colId,
        page,
        content,
        recommend
    })

    userTopic = (userId: number, fid?: number | undefined, recommend?: 1 | 0 | undefined, page?: number | undefined) => this.thread({
        authorid: userId,
        fid,
        recommend,
        page
    })

    userReplies = (userId: number, fid?: number | undefined, page?: number | undefined) => this.thread({
        authorid: userId,
        fid,
        page,
        searchpost: 1,
    })
    // 查询收藏夹内的主题列表
    favorTopic = (folderId: number, page?: number | undefined) => this.thread({favor: folderId, page})

    // 查询版面权限信息
    privilege = (fid: number) => this.instance.get("nuke.php", {params: {fid, __act: "view", __lib: 'view_privilege', __output: 11}}).then(res => res.data.data)
    /**
     * 通用请求
     * @param params 参数
     */
    thread = (params: any) => this.instance.get<NgaResponse<TopicListData>>("thread.php", {params: {...params, __output: 11}})
        .then(res => buildTopicListData(res.data.data))
}



